package com.tobby.camel.camelinaction.chapter3.enrich;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class EnrichTest extends CamelTestSupport {

	@Override
	protected RouteBuilder createRouteBuilder() {
		return new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("direct:start").pollEnrich("file:src/main/resources/data/name.txt", new AggregationStrategy() {
					@Override
					public Exchange aggregate(Exchange oldExchange,
							Exchange newExchange) {
						String original = oldExchange.getIn().getBody(String.class);
						String resource = newExchange.getIn().getBody(String.class);
						String mergeResult = original + ", " + resource;
						if (oldExchange.getPattern().isOutCapable()) {
							oldExchange.getOut().setBody(mergeResult);
						}
						return oldExchange;
					}
				}).to("mock:result");
			}
		};
	}

	@Test
	public void testEnrich() {
		MockEndpoint endpoint = getMockEndpoint("mock:result");
		endpoint.expectedBodiesReceived("Camel, Ni Hao");

		template.sendBody("direct:start", "Camel");
		try {
			assertMockEndpointsSatisfied();
		}
		catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
